﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;
using ERPStore.Tests.Controllers;
using ERPStore.Web.Html;
using ERPStore.Tests;
using ERPStore.Web.Extensions;

namespace ERPStore.Offers.Tests.Controllers
{
	[TestFixture]
	public class OffersControllerTests : ERPStore.Tests.TestBase
	{
		public OffersControllerTests()
		{

		}

		protected ERPStore.Offers.Services.AdminOffersService AdminOffersService { get; private set; }
		protected ERPStore.Services.ICatalogService CatalogService { get; private set; }

		[SetUp]
		public override void Initialize()
		{
			base.Initialize();
			AdminOffersService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Offers.Services.AdminOffersService>();
			CatalogService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ICatalogService>();
		}

		[TearDown]
		public virtual void TearDown()
		{
			base.TeardownHttpContext();
			var offerList = AdminOffersService.GetAllOffers();
			foreach (var item in offerList)
			{
				AdminOffersService.DeleteOffer(item);
			}
		}

		[Test]
		public void Null_Offer()
		{
			var offersController = base.CreateController<ERPStore.Offers.Controllers.OffersController>();

			var viewResult = offersController.Offer(null).Result as System.Web.Mvc.RedirectToRouteResult;

			Assert.AreEqual(viewResult.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.HOME);
		}

		[Test]
		public void Unknow_Offer()
		{
			var offersController = base.CreateController<ERPStore.Offers.Controllers.OffersController>();

			var viewResult = offersController.Offer(Guid.NewGuid().ToString()).Result as System.Web.Mvc.RedirectToRouteResult;

			Assert.AreEqual(viewResult.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.HOME);
		}

		[Test]
		public void Offer()
		{
			var offer = CreateOffer();

			var offersController = base.CreateController<ERPStore.Offers.Controllers.OffersController>();

			var viewResult = offersController.Offer(offer.Link).Result as System.Web.Mvc.ViewResult;

			var model = viewResult.Model as ERPStore.Models.ProductList;
			Assert.IsNotNull(model);
			Assert.AreEqual(model.Count, 1);
			Assert.IsNotNull(viewResult.ViewBag.Offer);

			var passedOffer = viewResult.ViewBag.Offer as Models.OfferEx;

			Assert.AreEqual(offer.Name, passedOffer.Name);

			var first = model.First();

			Assert.IsNotNull(first.BestPrice);
			Assert.IsNotNull(first.SelectedOffer);

			offer.ViewName = "offerview";
			AdminOffersService.SaveOffer(offer);

			viewResult = offersController.Offer(offer.Link).Result as System.Web.Mvc.ViewResult;

			Assert.AreEqual(viewResult.ViewName, "offerview");

		}

		[Test]
		public void Null_Product()
		{
			var offersController = base.CreateController<ERPStore.Offers.Controllers.OffersController>();
			var viewResult = offersController.Product(null, null) as System.Web.Mvc.RedirectToRouteResult;
			Assert.AreEqual(viewResult.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.HOME);
		}

		[Test]
		public void Unknown_Product()
		{
			var offersController = base.CreateController<ERPStore.Offers.Controllers.OffersController>();
			var viewResult = offersController.Product(Guid.NewGuid().ToString(), null) as System.Web.Mvc.RedirectToRouteResult;
			Assert.AreEqual(viewResult.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.HOME);
		}

		[Test]
		public void NotInOffer_Product()
		{
			var product = CatalogService.GetProductById(1);
			var offersController = base.CreateController<ERPStore.Offers.Controllers.OffersController>();
			var viewResult = offersController.Product(product.Code, product.Link) as System.Web.Mvc.RedirectResult;

			Assert.IsTrue(viewResult.Permanent);
			Assert.AreEqual(viewResult.Url, offersController.Url.Href(product));
		}

		[Test]
		public void Offer_Product()
		{
			var offer = CreateOffer();

			var offeredProduct = offer.ProductList.First();
			var product = CatalogService.GetProductById(offeredProduct.ProductId);

			var offersController = base.CreateController<ERPStore.Offers.Controllers.OffersController>();
			var viewResult = offersController.Product(product.Code, product.Link) as System.Web.Mvc.ViewResult;

			Assert.IsNotNull(viewResult);

			offer.ProductViewName = "productview";
			AdminOffersService.SaveOffer(offer);

			viewResult = offersController.Product(product.Code, product.Link) as System.Web.Mvc.ViewResult;

			Assert.AreEqual(viewResult.ViewName, "productview");
		}

		private Models.OfferEx CreateOffer()
		{
			var offer = AdminOffersService.CreateOffer();

			offer.Name = string.Format("OfferTest{0}", Guid.NewGuid().ToString());
			offer.Link = string.Format("test_link_{0}", Guid.NewGuid().ToString());
			offer.Description = "This is the best offer in univer now for you";
			offer.EndDate = DateTime.Now.AddDays(10);
			offer.Title = "The offer";

			AdminOffersService.SaveOffer(offer);

			var product = CatalogService.GetProductById(1);

			var offeredProduct = AdminOffersService.CreateOfferedProduct();
			offeredProduct.ProductId = 2;
			offeredProduct.SalePrice = product.SalePrice.Value - 10;
			offeredProduct.OfferId = offer.Id;

			AdminOffersService.SaveOfferedProduct(offeredProduct);

			return AdminOffersService.GetOfferById(offer.Id);
		}
	}
}
